import javax.media.opengl.GL;

// this class represent a box
public class Box {
	int box;
	double xsize=100;
	double ysize=100;
	double zsize=100;
	double xdisp=1000;
	double ydisp=0;
	double zdisp=1000;
	Portal portal;
	private Switcher switcher;
	private double downSpeed=0;
	
	// ctor
	public Box(Portal portal,Switcher switcher) {
		this.portal=portal;
		this.switcher=switcher;
		
	}
	
	

	public void draw(GL gl){
	    //switch check
		if (ydisp<1){
			
			switcher.checkActive(new Point3D(xdisp,ydisp,zdisp),2);
		}
		Point3D [] p =portal.checkPortalEnterance(new Point3D(xdisp, ydisp, zdisp));
		
	    if (p!=null){
	    	
	    	xdisp = p[0].x;
	    	ydisp = p[0].y;
	    	zdisp = p[0].z;
	    }

	    if (ydisp>=0){
	    	if (downSpeed<=1)
	    		downSpeed +=0.001;
	    	ydisp -= downSpeed;
	    }
	    
		gl.glPushMatrix();
		gl.glTranslated(xdisp,ydisp,zdisp );
		

		gl.glCallList(box);
		gl.glPopMatrix();
	}
	
		


	// creating a glList for drawing a box
	public void buildBox(GL gl) {
		box = gl.glGenLists(1);


		gl.glNewList(box, GL.GL_COMPILE);

		gl.glBegin(GL.GL_QUAD_STRIP);
			gl.glDisable(GL.GL_TEXTURE_2D);
			gl.glColor3f(1, 0, 0);
			gl.glVertex3d(0, 0, 0);
			gl.glVertex3d(0, ysize, 0);
	
			gl.glColor3f(0, 1, 0);
			gl.glVertex3d(xsize, 0, 0);
			gl.glVertex3d(xsize, ysize, 0);
	
			gl.glColor3f(0, 0, 1);
			gl.glVertex3d(xsize, 0, zsize);
			gl.glVertex3d(xsize, ysize, zsize);
	
			gl.glColor3f(1, 1, 0);
			gl.glVertex3d(0, 0, zsize);
			gl.glVertex3d(0, ysize, zsize);
	
			gl.glColor3f(1, 0, 0);
			gl.glVertex3d(0, 0, 0);
			gl.glVertex3d(0, ysize, 0);
			gl.glVertex3d(0, ysize, 0);
			gl.glVertex3d(0, ysize, zsize);
			gl.glVertex3d(xsize, ysize, 0);
			gl.glVertex3d(xsize, ysize, zsize);
			
			gl.glColor3f(1, 1, 1);
			gl.glEnable(GL.GL_TEXTURE_2D);
	    gl.glEnd();

	    gl.glEndList();
		
	}
}
